#include <stdlib.h>
#include "fleet.h"

//officer++++++++++++++++++++++++++++++
//constructor------------
officer::officer(string n, int p, int s, int c) {
    name = string(n);
    power = p;
    speed = s;
    cohes = c;
}
officer::officer(string n) {
    officer(n, (rand() % 5) + 1, (rand() % 5) + 1, (rand() % 5) + 1);
}
officer::~officer() {
}
//gLevel-----------------
// give the oficer the 
// indicated exp and
// check his level
// update stats if necesary
//-----------------------
int officer::gLevel(int e) {
    //increment exp & check level
    if (e != 0) {
        exp += e;
        int check = LEVEL_EQN(OFFICER_K, OFFICER_C, exp);
        if (check > level) {
            level = check;
            //update stats
            int bonus = (rand() % 5) + 3;
            int add = rand() % bonus;
            bonus -= add;
            power += add;
            add = rand() % bonus;
            bonus -= add;
            speed += add;
            cohes += bonus;
        }
    }
    //return level
    return level;
}
//======================================
//fCell+++++++++++++++++++++++++++++++++
//constructors-----------
fCell::fCell() {
    pos[0] = -1;
    pos[1] = -1;
    AP = 0;
    statPower = buffPower = 0;
    statSpeed = buffSpeed = 0;
    statCohes = buffCohes = 0;
    exper = curlevel = 0;
    shape = broad;
    facing = dirUp;
}
void fCell::setParent(fleet* p) {
    parent = p;
    parent->setBuffs(this);
    sNums = new int[numShipTps];
}
void fCell::initializeLoc(int x, int y) {
    pos[0] = x;
    pos[1] = y;
    fCellLib* lib = fCellLib::getFCellLib();
    lib->addFCell(this);
}
fCell::~fCell() {
    delete sNums;
}
//officer functions----------
void fCell::assignOfficer(officer* off) {
    if (cmd != NULL) {
        return;
    }
    cmd = off;
    buffPower += cmd->power;
    buffSpeed += cmd->speed;
    buffCohes += cmd->cohes;
}
officer* fCell::removeOfficer() {
    if (cmd == NULL) {
        return NULL;
    }
    officer* tmp = cmd;
    buffPower -= cmd->power;
    buffSpeed -= cmd->speed;
    buffCohes -= cmd->cohes;
    cmd = NULL;
    return tmp;
}
//composition manips---------
//formation--------
void fCell::formation(fltFormation form) {
    shape = form;
}
fltFormation fCell::formation() {
    return shape;
}
//numbers----------
void fCell::transfer(fCell* src, fCell* targ, int* ships){
    src->removeShips(ships);
    targ->addShips(ships);
}
void fCell::addShips(int* ships) {
    for (int i = 0; i < numShipTps; i++) {
        sNums[i] += ships[i];
    }
}
void fCell::removeShips(int* ships) {
    for (int i = 0; i < numShipTps; i++) {
        sNums[i] -= ships[i];
        if(sNums[i] < 0){
            sNums[i] = 0;
        }
    }
}
//commands-------------------
//auto commands----
void fCell::autoCommands(){
    while(AP > 0 )cqueue->doCommand();
}
void fCell::queueCommand(command* ccmd){
    cqueue->addCommand(ccmd);
}
//actions----------
voley* fCell::shoot(b_type bat){
    return NULL;
}
voley* fCell::takeDam(voley* vol){
    if(vol!=NULL){
        delete vol;
    }
    return NULL;
}
void fCell::move(int x, int y){
    //do ap calculations
    fCellLib* lib = fCellLib::getFCellLib();
    lib->moveCell(this, x, y);
    pos[0] = x;
    pos[1] = y;
}
//access-----------
int fCell::xLoc(){
    return pos[0];
}
int fCell::yLoc(){
    return pos[1];
}
void fCell::loc(int* x, int* y){
    *x = xLoc();
    *y = yLoc();
}
int fCell::level(){
    return 1;
}
int fCell::exp(int add){
    exper += add;
    level();
    return exper;
}
int fCell::cohes(int buff){
    buffCohes += buff;
    return statCohes + buffCohes;
}
int fCell::power(int buff){
    buffPower += buff;
    return statPower + buffPower;
}
int fCell::speed(int buff){
    buffSpeed += buff;
    return statSpeed + buffSpeed;
}
//======================================
//flett+++++++++++++++++++++++++++++++++
//constructors----------
fleet::fleet(empire* par){
    parent = par;
}
//======================================
